package com.gwsdk.paysdk.dialog.realname;

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.GWLog;

@SuppressLint("ValidFragment")
public class GWRealNameDialog extends DialogFragment {

    protected static final String TAG = "GWRealNameDialog";
    /** 上下文 */
    private Context con;

    /** 对话框内容 **/
    protected static final String KEY_MESSAGE  = "c_message";
    /** 是否能够取消  **/
    protected static final String KEY_CANCELABLE = "cancelable";

    /**
     * 创建CustomDialog, 使用{@link Builder}
     */
    public GWRealNameDialog(Context con) {
    	this.con = con;
    }

    /**
     * 创建CustomDialog, 使用{@link Builder}
     */
    public GWRealNameDialog() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View containerView = inflater.inflate(GWInflaterUtils.getLayout(con,"gw_dialog_realname_exit"), container, false);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                System.exit(0);
            }
        }, 2000);

        // 5,是否可以取消, 默认可以取消
        setCancelable(false);
        return containerView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置对话框的样式
        setStyle(STYLE_NO_FRAME, GWInflaterUtils.getIdByName(con,"style","gw_tip_dialog"));
    }

    @SuppressLint("NewApi")
	@Override
    public void onStart() {
        Window window = getDialog().getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        window.getAttributes().width = windowSize.x;
        window.getAttributes().height = windowSize.y;
        window.setGravity(Gravity.CENTER);

        super.onStart();
    }


    public static class Builder {

        /** 存放数据的容器  **/
        private Bundle mmBundle;

        public Builder() {
            mmBundle = new Bundle();
        }

        public Builder setMessage(CharSequence message) {
            mmBundle.putCharSequence(KEY_MESSAGE, message);
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mmBundle.putBoolean(KEY_CANCELABLE, cancelable);
            return this;
        }


        private GWRealNameDialog create(Context con) {
            final GWRealNameDialog dialog = new GWRealNameDialog(con);
            // 1,设置显示内容
            dialog.setArguments(mmBundle);

            return dialog;
        }

        public GWRealNameDialog show(Context con, FragmentManager fm) {
            if(fm == null) {
                GWLog.e(TAG, "show error : fragment manager is null.");
                return null;
            }
            GWRealNameDialog dialog = create(con);
            GWLog.d(TAG, "show custom dialog.");
            
            FragmentTransaction ft = fm.beginTransaction();
			ft.add(dialog, TAG);
	        ft.commitAllowingStateLoss();
            return dialog;
        }
    }
}
